package com.zhangyu.shadow.core.net.interceptors;

import android.support.annotation.NonNull;

import com.zhangyu.shadow.core.util.storage.ShadowPreference;

import java.io.IOException;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @author zhangyu
 * @date 2017/11/13
 */

public final class AddCookieInterceptor extends BaseInterceptor {
    @Override
    public Response intercept(@NonNull Chain chain) throws IOException {
        final Request.Builder requestBuilder = chain.request().newBuilder();
        Observable.just(ShadowPreference.getCustomAppProfile("cookie"))
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(@io.reactivex.annotations.NonNull String cookie) throws Exception {
                        //给原生的api请求附加上WebView拦截下来的cookie
                        requestBuilder.addHeader("Cookie", cookie);
                    }
                });
        return chain.proceed(requestBuilder.build());
    }
}
